package com.his.struts.dao.answer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.form.AnswerForm;
import com.his.struts.form.QuestionForm;
import com.his.struts.util.DbUtil;

/**
 * 回答提问
 * @author 肖伟亮
 * 2011-5-28
 */
public class AnswerImp implements AnswerDao {

	Logger log = Logger.getLogger(AnswerImp.class);
	DbUtil db = new DbUtil();

	/**
	 * 添加回复
	 * 
	 * @param qid
	 *            问题id
	 * @param answer
	 *            回答内容
	 * @param doctor
	 *            医生名字
	 * @return 是否添加成功
	 */
	public boolean addAnswer(int qid, String answer, String doctor) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "insert into answer(a_qid,a_answer,a_doctor,a_time) values(?,?,?,now())";
		list.add(qid);
		list.add(answer);
		list.add(doctor);

		int n = db.executeUpdate(sql, list);
		if (n > 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 得到问题详情
	 * @param qid 问题id
	 */
	public String getQuestion(int qid) {
		ArrayList list = new ArrayList();
		String sql="select uname,qtitle,qemail from questions as q join userinfo as u on q.qid = ? and u.uid = q.uid";
		list.add(qid);
		ResultSet rs = db.executeQuery(sql, list);
		String message = "";
		try {
			if(rs.next()){
				String uname = rs.getString("uname");
				String qtitle = rs.getString("qtitle");
				String qemail = rs.getString("qemail");
				message = uname + "-" + qtitle + "-" + qemail;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return message;
	}

	/**
	 * 得到将要回复的问题
	 * 
	 * @param qid
	 *            问题id
	 * @return 该问题的所有信息
	 */
	public QuestionForm answer(int qid) {
		ArrayList list = new ArrayList();
		String sql = "select * from questions where qid = ?";
		list.add(qid);
		ResultSet rs = db.executeQuery(sql, list);

		QuestionForm qform = new QuestionForm();
		try {
			if (rs.next()) {
				qform.setQid(rs.getInt("qid"));
				qform.setUid(rs.getInt("uid"));
				qform.setQtitle(rs.getString("qtitle"));
				qform.setQinfo(rs.getString("qinfo"));
				qform.setQsex(rs.getString("qsex"));
				qform.setQage(rs.getInt("qage"));
				qform.setQtype(rs.getString("qtype"));
				qform.setQemail(rs.getString("qemail"));
				qform.setQtime(rs.getString("qtime"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally{
			db.closeAll();
		}
		return qform;
	}

	/**
	 * 得到该问题的所有回答
	 * 
	 * @param qid
	 *            问题id
	 * @return 该问题的所有回答
	 */
	public List<AnswerForm> getAnswerBean(ResultSet rs) {
		ArrayList list = new ArrayList();
		try {
			while (rs.next()) {
				AnswerForm answer = new AnswerForm();
				answer.setA_qid(rs.getInt("a_qid"));
				answer.setA_answer(rs.getString("a_answer"));
				answer.setA_doctor(rs.getString("a_doctor"));
				answer.setA_time(rs.getString("a_time"));
				list.add(answer);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally{
			db.closeAll();
		}
		return list;
	}
	

	/**
	 * 得到所有对我提问的问题
	 * 
	 * @param type
	 *            我的专业
	 * @return 所有对我提问的问题
	 */
	public List<QuestionForm> getQuestionsMy(String type,String doctor) {

		ArrayList list = new ArrayList();
		String sql ="select * from questions where qtype = ? and qid not in " +
					"(select a_qid from answer where a_doctor = ?)";
		ArrayList slist = new ArrayList();
		slist.add(type);
		slist.add(doctor);
		ResultSet rs = db.executeQuery(sql, slist);
		// 封装bean
		List questionList = getMyQuestions(rs);
		db.closeAll();
		return questionList;
	}

	/**
	 * 查看我所有回答过到问题
	 * 
	 * @param doctor
	 *            医生名字
	 * @return 我回答的问题
	 */
	public List<QuestionForm> getMyAnswer(String doctor) {
		String sql ="select * from questions where qid in " +
					"(select a_qid from answer where a_doctor = ?)";
		ArrayList list = new ArrayList();
		list.add(doctor);
		ResultSet rs = db.executeQuery(sql, list);
		// 封装bean
		List questionList = getMyQuestions(rs);
		db.closeAll();
		return questionList;
	}
	
	/**
	 * 封装所有问题
	 * @param rs 问题游标
	 * @return 问题集合
	 */
	public List<QuestionForm> getMyQuestions(ResultSet rs) {
		ArrayList list = new ArrayList();
		try {
			while(rs.next()){
				QuestionForm qform = new QuestionForm();
				qform.setQid(rs.getInt("qid"));
				qform.setUid(rs.getInt("uid"));
				qform.setQtitle(rs.getString("qtitle"));
				qform.setQinfo(rs.getString("qinfo"));
				qform.setQsex(rs.getString("qsex"));
				qform.setQage(rs.getInt("qage"));
				qform.setQtype(rs.getString("qtype"));
				qform.setQemail(rs.getString("qemail"));
				qform.setQtime(rs.getString("qtime"));
				list.add(qform);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally{
			db.closeAll();
		}
		return list;
	}
	
	
}
